﻿using System;
using System.Collections.Generic;
using System.Linq;
using TestSystem.Model.Data;
using TestSystem.Model.Data.Repositories;
using TestSystem.Model.Exceptions;

namespace TestSystem.Model
{
    public class ModelFacade
    {
        private FoldersRepository foldersRepository = new FoldersRepository();
        private ToolsRepository toolsRepository = new ToolsRepository();
        private FrequencyTypesRepository frequencyTypesRepository = new FrequencyTypesRepository();
        private FrequencyRepository frequencyRepository = new FrequencyRepository();
        //private QuotesRepository quotesRepository = new QuotesRepository();

        public Folder CreateNewFolder(String name)
        {
            var folder = new Folder {Name = name};
            foldersRepository.Insert(folder);
            foldersRepository.Save();
            return folder;
        }
        public IQueryable<Folder> GetAllFolders()
        {
            return foldersRepository.FindAll();
        }
        public Tool CreateNewToolFor(Int32 parentId, String name, String description)
        {
            var parent = foldersRepository.GetById(parentId);
            if (parent == null)
                throw new NoSuchEntityException();

            var tool = new Tool {Name = name, FolderId = parent.Id, Description = description};
            toolsRepository.Insert(tool);
            toolsRepository.Save();

            return tool;
        }
        public void DeleteFolder(Int32 Id)
        {
            var folder = foldersRepository.GetById(Id);
            foldersRepository.Delete(folder);
            foldersRepository.Save();
        }
        public void DeleteToolFromFolder(Int32 ToolId)
        {
            var currentTool = toolsRepository.GetById(ToolId);
            if (currentTool == null)
                throw new NoSuchEntityException();
            toolsRepository.Delete(currentTool);
            frequencyRepository.Delete(ToolId);
            frequencyRepository.Save();
        }
        public IEnumerable<FrequencyType> CreateFrequencyTypesForTool(int ToolId)
        {
            frequencyRepository.Insert(ToolId);
            frequencyRepository.Save();
            return frequencyTypesRepository.FindAll();
        }
        public IEnumerable<FrequencyType> GetFrequencyTypes()
        {
            return frequencyTypesRepository.FindAll();
        }
        public Folder RenameFolder(string value, Int32 FolderId)
        {
            var folder = foldersRepository.GetById(FolderId);
            if (folder == null)
                throw new NoSuchEntityException();
            foldersRepository.Rename(folder, value);
            foldersRepository.Save();
            return folder;
        }
        public Tool RenameTool(string value,string description, int toolId)
        {
            var tool = toolsRepository.GetById(toolId);
            if (tool == null)
                throw new NoSuchEntityException();
            ToolsRepository.Rename(tool, value, description);
            toolsRepository.Save();
            return tool; 
        }

        public static void AddNewQuote( Int32 toolId, Int32 freqType, Int16 type, Quote quote )
        {
            QuotesRepository.AddNew(quote.DateTime, quote.Open, quote.High,quote.Low,quote.Close,
                                    quote.Volume,type, toolId,quote.Price,freqType);
        }
        public static void EditCurrentQuote (Int32 quoteId, Quote quote)
        {
            QuotesRepository.Update(quote.DateTime,quote.Open,quote.High,quote.Low,quote.Close,quote.Volume,quote.Price,quoteId);
        }
        public static void DeleteCurrentQuote (Int32 quoteId)
        {
            QuotesRepository.Delete(quoteId);
        }
    }
}
